import request from '@/utils/request'
import { RequestParams } from 'axios-mapper'
import type {
  ICodeModel,
  IUserState,
  IUserModel,
  IUserInfoResponse,
  ILoginResponse,
} from '@/model/userModel'

enum USER_API_URL {
  LOGIN_URL = '/auth/login',
  REGISTER_URL = '/auth/register',
  REFRESH_URL = '/auth/refresh',
  GET_USER_INFO_URL = '/system/user/getInfo',
  LOGOUT_URL = '/auth/logout',
  GET_LOGIN_ANTH_CODE_URL = '/code',
}

/**
 * 用户登录
 * @param userInfo
 * @returns Promise<ILoginResponse>
 */
export function login(userInfo: IUserModel): Promise<ILoginResponse> {
  // return request.post<any, ILoginResponse>(USER_API_URL.LOGIN_URL, userInfo)
  return request.request<any, ILoginResponse>(USER_API_URL.LOGIN_URL, {
    headers: {
      isToken: false,
    },
    method: 'post',
    data: userInfo,
  })
}

/**
 * 用户注册
 * @param data
 * @returns
 */
export function register(userInfo: IUserModel) {
  return request({
    url: USER_API_URL.REGISTER_URL,
    headers: {
      isToken: false,
    },
    method: 'post',
    data: userInfo,
  })
}

/**
 * 刷新方法
 * @returns Promise<any>
 */
export function refreshToken(): Promise<any> {
  return request.post<any, any>(USER_API_URL.REFRESH_URL)
}

/**
 * 获取用户详细信息
 * @returns Promise<IUserInfoResponse>
 */
export function getUserDetail(): Promise<IUserInfoResponse> {
  return request.get<any, IUserInfoResponse>(USER_API_URL.GET_USER_INFO_URL)
}

/**
 * 用户退出登录
 * @returns Promise<any>
 */
export function logout(): Promise<any> {
  return request.delete<any, any>(USER_API_URL.LOGOUT_URL)
}

/**
 * 获取图片验证码
 * @returns Promise<ICodeModel>
 */
export function getCodeImg(): Promise<ICodeModel> {
  return request.get<any, ICodeModel>(USER_API_URL.GET_LOGIN_ANTH_CODE_URL, {
    headers: {
      isToken: false,
    },
    method: 'get',
    timeout: 20000,
  })
  //
}
